package com.itheima.controller;

import com.itheima.Log.Log;
import com.itheima.common.PageBean;
import com.itheima.common.R;
import com.itheima.dto.EmpExprDto;
import com.itheima.dto.EmpQueryDto;
import com.itheima.dto.EmpUpdateDto;
import com.itheima.mapper.EmpMapper;
import com.itheima.pojo.Emp;
import com.itheima.pojo.EmpExpr;
import com.itheima.service.EmpService;
import com.itheima.vo.EmpVo;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("emps")
public class EmpController {
    @Autowired
    EmpService empService;

    @GetMapping
    public R pageQuery(EmpQueryDto dto) {
        PageBean pageBean = empService.pageQuery(dto);
        return R.ok(pageBean);
    }

    @PostMapping
    @Log
    public R add(@RequestBody EmpExprDto dto) {
        empService.add(dto);
        return R.ok();

    }

    @GetMapping("/{id}")
    public R selectid(@PathVariable Integer id) {
        EmpVo vo = empService.selectid(id);
        return R.ok(vo);
    }

    @PutMapping
    @Log
    public R update(@RequestBody EmpExprDto dto) {
        empService.update(dto);
        return R.ok();
    }

    @DeleteMapping
    @Log
    public R deleteid(@RequestParam List<Integer> ids){
        empService.deleteid(ids);
        return R.ok();
    }

    @GetMapping("/list")
    public R selectall(){
        List<Emp> emps = empService.selectall();
        return R.ok(emps);
    }

    @PutMapping("/changePassword")
    public R changepassword(@RequestBody EmpUpdateDto dto){
        empService.empupdate(dto);
        return R.ok();
    }

}
